package persistence;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import model.TCC;

public class TCCDaoImpl implements TCCDao {
	Connection c;

	public TCCDaoImpl() {
		GenericDao gDao = new GenericDao();
		c = gDao.getConnection();
	}

	@Override
	public void salvaTCC(TCC tcc) throws SQLException {
		String sql = "INSERT INTO TCC VALUES (?,?)";
		PreparedStatement ps = c.prepareStatement(sql);

		ps.setInt(1, tcc.getIdTCC());
		ps.setString(2, tcc.getTema());

		ps.execute();
		ps.close();

	}

	@Override
	public void editaTCC(TCC tcc) throws SQLException {
		String sql = "UPDATE TCC SET tema = ? WHERE idTCC = ?";
		PreparedStatement ps = c.prepareStatement(sql);
		ps.setString(1, tcc.getTema());
		ps.setInt(2, tcc.getIdTCC());
		ps.execute();
		ps.close();

	}

	@Override
	public void excluiTCC(TCC tcc) throws SQLException {
		String sql = "DELETE TCC WHERE idTCC = ?";
		PreparedStatement ps = c.prepareStatement(sql);
		ps.setInt(1, tcc.getIdTCC());
		ps.execute();
		ps.close();

	}

	@Override
	public TCC pesquisaTCC(TCC tcc) throws SQLException {
		TCC t = new TCC();
		String sql = "SELECT idTCC, tema FROM TCC WHERE idTCC = ?";
		PreparedStatement ps = c.prepareStatement(sql);
		ps.setInt(1, tcc.getIdTCC());
		ResultSet rs = ps.executeQuery();
		if (rs.next()) {
			t.setIdTCC(rs.getInt("idTCC"));
			t.setTema(rs.getString("tema"));

		}
		ps.close();
		rs.close();
		return t;
	}
}
